<script>
import outMain from "@/components/outMain.vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { Integralgoods, goodsCheckStatus, goodsDel } from "@/http/api";

export default {
  components: { outMain },
  name: "",
  data() {
    return {
      tableData: [],
      statusArr: [
        { label: "全部", value: "" },
        { label: "启用", value: "1" },
        { label: "关闭", value: "2" },
      ],
      queryData: {
        goods_status: "",
        goods_name: "",
        page: 1,
        per_page: 10,
      },
      total: 0,
    };
  },
  props: {},
  setup() {},
  methods: {
    addFn(id) {
      this.$router.push({
        path: "/integralProductAdd",
        query: { id: id, page: id ? this.queryData.page : undefined },
      });
    },
    deleteFn(row) {
      ElMessageBox.confirm("确定删除选择的商品吗?", "删除提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        goodsDel({ integral_goods_id: row.integral_goods_id }).then((res) => {
          if (res.data.code == 1) {
            ElMessage({
              type: "success",
              message: "删除成功",
            });
            this.getData();
          }
        });
      });
    },
    questCheck(data) {
      goodsCheckStatus(data).then((res) => {
        if (res.data.code == 1) {
          ElMessage({
            type: "success",
            message: "操作成功",
          });
          this.getData();
        }
      });
    },
    checkStatus(row, type) {
      let str = type == 1 ? "启用" : "关闭";
      ElMessageBox.confirm(`确定${str}选择的商品吗?`, "删除提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        let data = {
          integral_goods_id: row.integral_goods_id,
          status: type,
        };
        this.questCheck(data);
      });
    },
    getData() {
      Integralgoods(this.queryData).then((res) => {
        if (res.data.code == 1) {
          this.tableData = res.data.data.data;
          this.total = res.data.data.total;
        }
      });
    },
  },
  mounted() {},
  created() {
    this.queryData.page = Number(localStorage.getItem("boot_page")) || 1;

    this.getData();
  },
};
</script>
<template>
  <outMain>
    <div class="tableCont">
      <div class="tableName flexCont">
        <div>积分商品列表（{{ total }}）</div>
      </div>
      <div class="tableSerch">
        <div class="serchLeft">
          <btn value="新增" @click="addFn()"></btn>
        </div>
        <div class="serchRight">
          <el-form class="flexCenter" :inline="true">
            <el-form-item>
              <el-input
                clearable
                @clear="getData"
                @keyup.enter="getData"
                v-model="queryData.goods_name"
                placeholder="请输入商品名称"
              />
            </el-form-item>
            <el-form-item>
              <el-select
                v-model="queryData.goods_status"
                clearable
                @change="getData"
                placeholder="请选择商品状态"
              >
                <el-option
                  v-for="item in statusArr"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
            <btn value="搜索" @click="getData"></btn>
          </el-form>
        </div>
      </div>
      <el-table class="tableMain" :data="tableData" stripe style="width: 100%">
        <el-table-column prop="comName" show-overflow-tooltip label="商品图片">
          <template #default="{ row }">
            <img class="userImg" :src="row.goods_img" />
          </template>
        </el-table-column>
        <el-table-column
          prop="goods_name"
          show-overflow-tooltip
          label="商品名称"
          width="250"
        />
        <el-table-column prop="is_yj" show-overflow-tooltip label="商品类型">
          <template #default="{ row }">
            <div class="tagGroup">
              <el-tag v-if="row.is_yj == 1">虚拟商品</el-tag>
              <el-tag type="info" v-if="row.is_yj == 2">实物商品</el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          prop="integral_sum"
          show-overflow-tooltip
          label="兑换积分"
        />
        <el-table-column
          prop="sign_goods_sum"
          show-overflow-tooltip
          label="已兑换"
        />
        <el-table-column
          prop="goods_sum"
          show-overflow-tooltip
          label="库存数量"
        />
        <el-table-column show-overflow-tooltip label="状态">
          <template #default="{ row }">
            <div class="tagGroup">
              <el-tag v-if="row.goods_status == 1">开启</el-tag>
              <el-tag type="info" v-if="row.goods_status == 2">关闭</el-tag>
              <el-tag type="danger" v-if="row.goods_status == 3">已到期</el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          width="120"
          prop="expire_time"
          show-overflow-tooltip
          label="到期时间"
        />
        <el-table-column
          width="180"
          prop="create_time"
          show-overflow-tooltip
          label="创建时间"
        />
        <el-table-column prop="Name" width="140" label="操作">
          <template #default="{ row }">
            <div class="tableCtrl">
              <div
                class="blueText ctrlText"
                @click="addFn(row.integral_goods_id)"
              >
                编辑
              </div>
              <div class="redText ctrlText" @click="deleteFn(row)">删除</div>
              <div
                class="blueText ctrlText"
                v-if="row.goods_status == 2"
                @click="checkStatus(row, 1)"
              >
                开启
              </div>
              <div
                class="redText ctrlText"
                v-if="row.goods_status == 1"
                @click="checkStatus(row, 2)"
              >
                关闭
              </div>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="pageCont">
        <el-pagination
          v-model:currentPage="queryData.page"
          v-model:page-size="queryData.per_page"
          background
          layout="total, prev, pager, next, jumper"
          :total="total"
          @size-change="getData"
          @current-change="getData"
        />
      </div>
    </div>
  </outMain>
</template>
<style scoped lang="less"></style>
